package com.inqbarna.tablefixheaders.samples.slice;

import com.inqbarna.tablefixheaders.TableFixHeaders;
import com.inqbarna.tablefixheaders.samples.ResourceTable;
import com.inqbarna.tablefixheaders.samples.adapters.SampleTableAdapter;
import com.inqbarna.tablefixheaders.utils.ResUtils;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;

public class StyleTable extends AbilitySlice {

    TableFixHeaders tableFixHeaders;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_table, null, false);

        tableFixHeaders = (TableFixHeaders) rootLayout.findComponentById(ResourceTable.Id_table);
        try {
            tableFixHeaders.setAdapter(new MyAdapter(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.setUIContent(rootLayout);
    }

    public class MyAdapter extends SampleTableAdapter {

        private final int width;

        private final int height;

        public MyAdapter(Context context) {
            super(context);

            ResourceManager resources = context.getResourceManager();

            width = (int) ResUtils.getDimen(context, ResourceTable.Float_table_width);
            height = (int) ResUtils.getDimen(context, ResourceTable.Float_table_height);
        }

        @Override
        public int getRowCount() {
            return 10;
        }

        @Override
        public int getColumnCount() {
            return 6;
        }

        @Override
        public int getWidth(int column) {
            return width;
        }

        @Override
        public int getHeight(int row) {
            return height;
        }

        @Override
        public String getCellString(int row, int column) {
            return "Lorem (" + row + ", " + column + ")";
        }

        @Override
        public int getLayoutResource(int row, int column) {
            final int layoutResource;
            switch (getItemViewType(row, column)) {
                case 0:
                    layoutResource = ResourceTable.Layout_item_table1_header;
                    break;
                case 1:
                    layoutResource = ResourceTable.Layout_item_table1;
                    break;
                default:
                    throw new RuntimeException("");
            }
            return layoutResource;
        }

        @Override
        public int getItemViewType(int row, int column) {
            if (row < 0) {
                return 0;
            } else {
                return 1;
            }
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
    }

    @Override
    protected void onBackPressed() {
        tableFixHeaders.setAdapter(null);
        tableFixHeaders = null;
        super.onBackPressed();

    }
}